/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.riversoft.weixin.gateway;

import com.riversoft.weixin.common.oauth2.AccessToken;
import com.riversoft.weixin.gateway.service.WxUserService;
import com.riversoft.weixin.gateway.service.bean.RegistInfo;
import com.riversoft.weixin.gateway.service.model.WxUserBind;
import com.riversoft.weixin.mp.base.AppSettingFactory;
import com.riversoft.weixin.mp.oauth2.MpOAuth2s;
import com.riversoft.weixin.mp.user.Users;
import com.riversoft.weixin.mp.user.bean.User;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author johns
 */
public class WxUserBindController {
    private static Logger logger = LoggerFactory.getLogger(WxUserBindController.class);
    
    @Autowired
    WxUserService wxUserService;
    
    
    @Autowired
    private AESUtil aesUtil;
    /**
     * 消费者需要注册
     * 
     * 默认注册为社会公众
     * 
     * 查询数据库 openid 是否对应 零售户或者执法人员或者对应消费者
     * 
     * 
     * @param code
     * @param state
     * @param response
     * @throws IOException 
     */
    @RequestMapping("/wx/forward")
    public void forward(@RequestParam(value="code") String code,
            @RequestParam(value="state") String state,
            HttpServletResponse response
            ) throws IOException {
        AppSettingFactory appSetting = AppSettingFactory.defaultSettings();
        MpOAuth2s oauth = MpOAuth2s.with(appSetting);
        AccessToken token = oauth.getAccessToken(code);
        logger.debug("请求code为{} state为{}，获取到openid:{}", code, state, token.getOpenId());
        WxUserBind user = wxUserService.findById(token.getOpenId());
        if(user==null){
            //没有进行注册绑定跳转到绑定页面
            User wxUser = Users.defaultUsers().get(token.getOpenId());
            logger.debug("获取到的用户信息:用户昵称{},用户头像路径：{}",wxUser.getNickName(),wxUser.getHeadImgUrl());
            response.sendRedirect("https://zhsen.applinzi.com/index.html?id="+aesUtil.encrypt(token.getOpenId()));
        }else{
            User wxUser = Users.defaultUsers().get(user.getOpenId());
            //用户已经进行绑定操作 直接跳转到主页面
            response.sendRedirect("https://zhsen.applinzi.com/index.html?id="+aesUtil.encrypt(token.getOpenId()));
        }
    }
    @RequestMapping("/wx/bind")
    public void bind(@RequestBody RegistInfo info){
        String openId = aesUtil.decrypt(info.getId());
        WxUserBind user = new WxUserBind();
        user.setOpenId(openId);
        user.setTel(info.getTel());
    }
}
